﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MHYS.DEMO.Model;
using MEYS.DEMO.BLL;
namespace Demo
{
    public partial class Default : System.Web.UI.Page
    {

        protected static DateTime sysTime;
        protected static UserInfo user = new UserInfo();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                BindData();
                if (this.Session["User"] == null)
                {
                    this.Response.Redirect("Default.aspx");
                }
                else
                {
                    user = this.Session["User"] as UserInfo;
                    this.txtUserName.Text = user.UserName;
                    this.txtPwd.Text = user.UserPwd;
                    this.txtTrueName.Text = user.TrueName;
                    this.txtAddress.Text = user.Address;
                }
            }
        }

        /// <summary>
        /// 绑定初始数据
        /// </summary>
        private void BindData()
        {
            this.ddlRole.DataSource = RankInfoManager.ShowUserInfo();
            this.ddlRole.DataTextField = "RName";
            this.ddlRole.DataValueField = "Rid";
            this.ddlRole.DataBind();
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            user.UserName = this.txtUserName.Text;
            user.UserPwd = this.txtPwd.Text;
            user.Rid = Convert.ToInt32(this.ddlRole.SelectedValue);
            user.Address = this.txtAddress.Text;
            user.TrueName = this.txtTrueName.Text;
            //1.更新数据
            if (UserInfoManager.UpdateUserInfo(user))
            {
                this.Response.Write("<script>alert('更新成功')</script>");
            }
            else
            {
                this.Response.Write("<script>alert('更新失败')</script>");

            }
            //2.绑定数据
            BindData();//重新绑定数据
            user = UserInfoManager.ShowUserInfo(this.txtUserName.Text);
            this.txtUserName.Text = user.UserName;
            this.txtPwd.Text = user.UserPwd;
            this.txtTrueName.Text = user.TrueName;
            this.txtAddress.Text = user.Address;
        }

        protected void ddlRole_SelectedIndexChanged(object sender, EventArgs e)
        {

        }


    }
}